package homework.view;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import homework.entity.User;
import homework.service.Usermanage;

public class RegisterFrame extends JFrame{
    JLabel l_username, l_password1, l_password2, l_type;
    JTextField t_username;
    JPasswordField p_password1, p_password2;
    JComboBox<String> c_type;
    JButton b_reset, b_register;
    Usermanage um=new Usermanage();
    
    public RegisterFrame() {
    	setTitle("快递物流平台-注册页面");
    	setSize(400,500);
    	setLocation(800,200);
    	init();
    	setVisible(true);
    }
    public void init() {
    	l_username = new JLabel("账号：",JLabel.RIGHT);
    	l_password1 = new JLabel("密码：",JLabel.RIGHT);
    	l_password2 = new JLabel("确认密码：",JLabel.RIGHT);
    	l_type = new JLabel("用户类型：",JLabel.RIGHT);
    	t_username = new JTextField();
    	p_password1 = new JPasswordField();
    	p_password2 = new JPasswordField();
    	String[] tyString = {"用户","快递员"};
    	c_type = new JComboBox<String>(tyString);
    	b_reset = new JButton("重置");
    	b_register = new JButton("注册");
    	
    	b_reset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				t_username.setText("");
				p_password1.setText("");
				p_password2.setText("");
			}
		});
    	
    	
    	b_register.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				if(!(p_password1.getPassword().equals(p_password2.getPassword()))) {
					String passwordString = new String(p_password1.getPassword());
					String typeString = (String)c_type.getSelectedItem();
					String username = t_username.getText();
					
					User user = new User(username,passwordString,typeString);
					
					um.register(user);
					
					JOptionPane.showMessageDialog(null,"注册成功！");
					
				}else {
					JOptionPane.showMessageDialog(null, "您两次输入的密码不一致，请从新输入！");
				}
			}
		});
    	
    	this.setLayout(new GridLayout(5,2,5,5));
    	
    	add(l_username);
    	add(t_username);
    	add(l_password1);
    	add(p_password1);
    	add(l_password2);
    	add(p_password2);
    	add(l_type);
    	add(c_type);
    	
        add(b_reset);
        add(b_register);
    	
    }
    
    
}


